home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / jDictionary 1.8 / jdictionary-1_8-win.exe / src-1_8 / info / jdictionary / JDictionary.java < prev    next >
Encoding:
Java Source  |  2002-08-11  |  11.1 KB  |  346 lines

  1. /*
  2.  * 01/09/2002 - 20:43:57
  3.  *
  4.  * JDictionary.java -
  5.  * Copyright (C) 2002 Csaba KertΘsz
  6.  * kcsaba@jdictionary.info
  7.  * www.jdictionary.info
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Lesser General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU Lesser General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU Lesser General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. package info.jdictionary;
  25.  
  26. import info.jdictionary.pluginstuff.PluginManager;
  27. import info.jdictionary.pluginstuff.PluginDownloader;
  28. import info.jdictionary.pluginstuff.PluginStatusChanger;
  29. import info.jdictionary.gui.UpgradeAgent;
  30. import info.jdictionary.gui.AboutPanel;
  31. import info.jdictionary.gui.NoPluginsPanel;
  32. import info.jdictionary.gui.AllPluginsDeactivatedPanel;
  33. import java.io.File;
  34. import java.util.ResourceBundle;
  35. import java.util.Locale;
  36. import javax.swing.JFrame;
  37. import javax.swing.JPanel;
  38. import java.net.URL;
  39. import java.net.Authenticator;
  40. import java.net.PasswordAuthentication;
  41.  
  42.  
  43. public class JDictionary {
  44.  
  45.     private static ResourceBundle JDictionaryResources;
  46.     private static JDictionary jDictionary;
  47.  
  48.     private Prefs prefs;
  49.     private PluginManager pluginManager;
  50.     private PluginDownloader pluginDownloader;
  51.     private PluginStatusChanger pluginStatusChanger;
  52.     private NewsAgent newsAgent;
  53.     private UpgradeAgent upgradeAgent;
  54.     private Preferences preferences;
  55.     private JDictionaryFrame frame;
  56.     private DefaultFrameAssembler assembler;
  57.     private NewJDictionaryVersionChecker newJDictionaryVersionChecker = new NewJDictionaryVersionChecker();
  58.     private NewsChecker newsChecker = new NewsChecker(this);
  59.     private String baseDir;
  60.  
  61.  
  62.     public JDictionary() {
  63.         prefs = loadPrefs();
  64.         initProxy();
  65.         frame = new JDictionaryFrame();
  66.         pluginManager = new PluginManager(this);
  67.         assembler = new DefaultFrameAssembler(this, frame);
  68.         pluginManager.scanPlugins();
  69.         if(prefs.CheckingForUpgrade)
  70.             newJDictionaryVersionChecker.start(); //checking for new jdictionary version
  71.         if(prefs.CheckingForNews)
  72.             newsChecker.start(); //checking for news
  73.     }
  74.  
  75.  
  76.     public JPanel getSplashScreen() {
  77.         return new AboutPanel();
  78.     }
  79.  
  80.  
  81.     public JPanel getNoPluginInstalledScreen() {
  82.         return new NoPluginsPanel();
  83.     }
  84.  
  85.  
  86.     public JPanel getAllPluginsDeactivatedScreen() {
  87.         return new AllPluginsDeactivatedPanel();
  88.     }
  89.  
  90.  
  91.     public PluginStatusChanger getPluginStatusChanger() {
  92.         if(pluginStatusChanger == null) {
  93.             pluginStatusChanger = new PluginStatusChanger(this);
  94.             Thread pluginStatusChangerThread = new Thread(pluginStatusChanger);
  95.             pluginStatusChangerThread.start();
  96.         }
  97.         return pluginStatusChanger;
  98.     }
  99.  
  100.  
  101.     public PluginDownloader getPluginDownloader() {
  102.         if(pluginDownloader == null || pluginDownloader.isSuccess() == false) {
  103.             pluginDownloader = new PluginDownloader(this);
  104.             Thread pluginDownloaderThread = new Thread(pluginDownloader);
  105.             pluginDownloaderThread.start();
  106.         }
  107.         return pluginDownloader;
  108.     }
  109.  
  110.  
  111.     public NewsAgent getNewsAgent() {
  112.         if(newsAgent == null || newsAgent.isSuccess() == false) {
  113.             newsAgent = new NewsAgent(this);
  114.             Thread newsAgentThread = new Thread(newsAgent);
  115.             newsAgentThread.start();
  116.         }
  117.         return newsAgent;
  118.     }
  119.  
  120.  
  121.     public UpgradeAgent getUpgradeAgent() {
  122.         if(upgradeAgent == null || upgradeAgent.isSuccess() == false)
  123.             upgradeAgent = new UpgradeAgent(this);
  124.         return upgradeAgent;
  125.     }
  126.  
  127.  
  128.     public Preferences getPreferencesScreen() {
  129.         if(preferences == null)
  130.             preferences = new Preferences(this);
  131.         return preferences;
  132.     }
  133.  
  134.  
  135.     public NewJDictionaryVersionChecker getNewJDictionaryVersionChecker() {
  136.         return newJDictionaryVersionChecker;
  137.     }
  138.  
  139.  
  140.     public NewsChecker getNewsChecker() {
  141.         return newsChecker;
  142.     }
  143.  
  144.  
  145.     public DefaultFrameAssembler getAssembler() {
  146.         return assembler;
  147.     }
  148.  
  149.  
  150.     public void closeJDictionary() {
  151.         savePrefs();
  152.         pluginManager.removeAllPlugins();
  153.         System.exit(0);
  154.     }
  155.  
  156.  
  157.     public void savePrefs() {
  158.         assembler.savePrefs();
  159.         //other items in prefs have already been adjusted
  160.         prefs.lastSelectedPlugin = pluginManager.getSelectedPlugin();
  161.         prefs.lastSelectedSubPluginName = pluginManager.getSelectedSubPlugin();
  162.         Prefs.savePrefs(prefs, getSavedDir() + getFileSeparator() + getString("SavedPrefsFileName"));
  163.     }
  164.  
  165.  
  166.     public Prefs loadPrefs() {
  167.         return Prefs.loadPrefs(getSavedDir() + getFileSeparator() + getString("SavedPrefsFileName"));
  168.     }
  169.  
  170.  
  171.     public PluginManager getPluginManager() {
  172.         return pluginManager;
  173.     }
  174.  
  175.  
  176.     public Prefs getPrefs() {
  177.         return prefs;
  178.     }
  179.  
  180.  
  181.     public String getBaseDir() {
  182.         if(baseDir != null)
  183.             return baseDir;
  184.         URL url = getClass().getResource("/" + getString("ResourcesDirName"));
  185.         String temp = url.toString();
  186.         StringBuffer s = new StringBuffer();
  187.         for(int i = 0; i < temp.length(); i++) {
  188.             if(temp.charAt(i) != '%')
  189.                 s.append(temp.charAt(i));
  190.             else {
  191.                 s.append(" ");
  192.                 i = i + 2;
  193.             }
  194.         }
  195.         int e = s.toString().indexOf("jdictionary.jar");
  196.         if(e < 10)
  197.             return System.getProperty("user.dir");
  198.         baseDir = s.toString().substring(9, e-1);
  199.         return baseDir;
  200.     }
  201.  
  202.  
  203.     public String getPluginDirPath() {
  204.         File dir = new File(getSavedDir() + getFileSeparator() + getString("PluginDirName"));
  205.         if(!dir.exists() || !dir.isDirectory())
  206.             dir.mkdir();
  207.         return dir.getAbsolutePath();
  208.     }
  209.  
  210.  
  211.     public String getGlobalPluginDirPath() {
  212.          File dir = null;
  213.          try {
  214.              dir = new File(getBaseDir() + getFileSeparator() + getString("PluginDirName"));
  215.              if(!dir.exists() || !dir.isDirectory())
  216.                  if(!dir.mkdir())
  217.                      return null;
  218.          }
  219.          catch(java.lang.Exception e) {return null;}
  220.          return dir.getAbsolutePath();
  221.      }
  222.  
  223.  
  224. //////////static methods/////////////////////////////////
  225.  
  226.  
  227.      public static void main(String[] args) {
  228.          jDictionary = new JDictionary();
  229.     }
  230.  
  231.  
  232.     public static JDictionary getJDictionary() {
  233.         return jDictionary;
  234.     }
  235.  
  236.  
  237.     public static String getString(String key) {
  238.         if (JDictionaryResources == null)
  239.             JDictionaryResources = ResourceBundle.getBundle("info.jdictionary.resource.JDictionaryResources",new Locale("en","en"));
  240.         return JDictionaryResources.getString(key);
  241.     }
  242.  
  243.  
  244.     public static float getJDictionaryVersion() {
  245.         return Float.parseFloat(getString("JDictionaryVersion"));
  246.     }
  247.  
  248.  
  249.     public static String getUserHome() {
  250.         return System.getProperty("user.home");
  251.     }
  252.  
  253.  
  254.     public static String getFileSeparator() {
  255.         return System.getProperty("file.separator");
  256.     }
  257.  
  258.  
  259.     public static String getLanguage() {
  260.         return System.getProperty("user.language");
  261.     }
  262.  
  263.  
  264.     public static String getSavedDir() {
  265.         File dir = new File(getUserHome() + getFileSeparator() + getString("SavedDirName"));
  266.         if(!dir.exists() || !dir.isDirectory())
  267.             dir.mkdir();
  268.         return dir.getAbsolutePath();
  269.     }
  270.  
  271.  
  272.     public void initProxy() {
  273.         // cleaning up proxy settings
  274.         System.getProperties().remove("proxySet");
  275.         System.getProperties().remove("proxyHost");
  276.         System.getProperties().remove("proxyPort");
  277.         System.getProperties().remove("socksProxySet");
  278.         System.getProperties().remove("socksProxyHost");
  279.         System.getProperties().remove("socksProxyPort");
  280.         Authenticator.setDefault(null);
  281.  
  282.         if (prefs.usingHttpProxy) {
  283.             //set proxy host
  284.             String host = prefs.httpProxyServer;
  285.             if (host == null)
  286.                 return;
  287.             System.setProperty("proxyHost", host);
  288.  
  289.             // set proxy port
  290.             String port = prefs.httpProxyPort;
  291.             if (port != null && port.length() > 0)
  292.                 System.setProperty("proxyPort", port);
  293.             else
  294.                 System.setProperty("proxyPort", "80"); //http proxy default
  295.             System.setProperty("proxySet", "true");
  296.             // set proxy authentication
  297.             if(prefs.usingHttpProxyLogin) {
  298.                 String username = prefs.httpProxyLogin;
  299.                 char[] password = prefs.httpProxyPassword;
  300.  
  301.                 if(password == null)
  302.                     password = new char[0];
  303.  
  304.                 if(username == null || username.length() == 0) {
  305.                     Authenticator.setDefault(new ProxyAuthenticator(null));
  306.                 }
  307.                 else {
  308.                     PasswordAuthentication passwordAuthentication = new PasswordAuthentication(username, password);
  309.                     Authenticator.setDefault(new ProxyAuthenticator(passwordAuthentication));
  310.                 }
  311.             }
  312.         }
  313.  
  314.         if(prefs.usingSocksProxy) {
  315.         //set socks proxy host
  316.            String host = prefs.socksProxyServer;
  317.            if (host == null)
  318.                return;
  319.            System.getProperties().setProperty("socksProxyHost", host);
  320.  
  321.            // set socks proxy port
  322.            String port = prefs.socksProxyPort;
  323.            if (port != null && port.length() > 0)
  324.                System.getProperties().setProperty("socksProxyPort", port);
  325.            else
  326.                System.getProperties().setProperty("socksProxyPort", "1080");  //socks proxy default
  327.            System.getProperties().setProperty("socksProxySet", "true");
  328.            // set proxy authentication
  329.            if(prefs.usingSocksProxyLogin) {
  330.                String username = prefs.socksProxyLogin;
  331.                char[] password = prefs.socksProxyPassword;
  332.  
  333.                if(password == null)
  334.                    password = new char[0];
  335.  
  336.                if(username == null || username.length() == 0) {
  337.                    Authenticator.setDefault(new ProxyAuthenticator(null));
  338.                }
  339.                else {
  340.                    PasswordAuthentication passwordAuthentication = new PasswordAuthentication(username, password);
  341.                    Authenticator.setDefault(new ProxyAuthenticator(passwordAuthentication));
  342.                }
  343.            }
  344.         }
  345.     }
  346. }